package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.vo.DishDto;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    //起售停售

    @Override
    public R updateDishStatus(Integer status, List<Long> ids) {
        Dish dish = new Dish();
        dish.setStatus(status);
        for (Long id : ids) {
            dish.setId(id);
            dishMapper.updateById(dish);
        }

        return R.success("修改成功");
    }

    @Override
    public R getDishByCategoryId(Long categoryId) {
        //修改,包含菜品口味
        List<Dish> dishes = dishMapper.selectDishByCategoryId(categoryId);
        //创建最终返回集合
        List<DishDto> dishDtos = new ArrayList<>();
        /*!!!!注意：问题：dishDto不能放在循环外面，
        ！
        ！
        ！
        ！
        ！
        ！
        1
        beanutil每次复制完list里的所有数据都跟着变*/
        //遍历集合
        for (Dish dish : dishes) {
            //创建暂时的容器
            DishDto dishDto = new DishDto();
            //记录菜品
            BeanUtil.copyProperties(dish,dishDto);
            //根据菜品id获取菜品口味集合
            Long dishId = dish.getId();
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectFlavorsByDishId(dishId);
            dishDto.setFlavors(dishFlavors);
            //保存dto
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }
    //删除菜品

    @Override
    public R deleteDish(List<Long> ids) {
         dishMapper.deleteBatchIds(ids);
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(qw);
        }
        return R.success("删除成功");
    }


    //修改菜品数据
    @Override
    public R updateDish(DishDto dishDto) {
        Long dishId = dishDto.getId();
        dishMapper.updateById(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dishId);
        dishFlavorMapper.delete(qw);
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavorMapper.insert(flavor);
        }
        return R.success("修改成功");
    }



    //根据id查询菜品
    @Override
    public R selectDishById(Long id) {
        //根据id获取菜品
        Dish dish = dishMapper.selectDishById(id);
        //根据菜品id获取口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectFlavorsByDishId(id);
        //设置返回数据
        DishDto dishDto = new DishDto();
        BeanUtil.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavors);
        return R.success(dishDto);
    }



    //菜品分页查询
    @Override
    public R dishPage(Integer page, Integer pageSize, String name) {
        //---------------使用mp
        Page<Dish> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(!StrUtil.hasEmpty(name),Dish::getName,name);
        dishMapper.selectPage(iPage,qw);
        //记录总数与查询数据
        HashMap<String, Object> map = new HashMap<>();
        //获取总数
        long total = iPage.getTotal();
        map.put("total",(int)total);
        //获取菜品数据
        List<Dish> dishes = iPage.getRecords();
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : dishes) {
            //创建dto
            DishDto dishDto = new DishDto();
            //获取菜品id
            Long categoryId = dish.getCategoryId();
            //获取菜品名称
            LambdaQueryWrapper<Category> qwc = new LambdaQueryWrapper<>();
            qwc.eq(Category::getId,categoryId);
            Category category = categoryMapper.selectOne(qwc);
            String categoryName = category.getName();
            //复制属性至dto，设置菜品名称
            BeanUtil.copyProperties(dish,dishDto);
            dishDto.setCategoryName(categoryName);
            //保存进集合
            dishDtos.add(dishDto);
        }
        map.put("records",dishDtos);
        return R.success(map);
    }


    //添加菜品
    @Override
    public R saveDish(DishDto dishDto) {
        //添加菜品
        dishMapper.insert(dishDto);
        //取出菜品口味
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavorMapper.insert(flavor);
        }
        return R.success("添加成功");
    }


}
